<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\User;
use app\admin\model\Coupon as CouponModer;
use app\admin\model\UserCoupon;
use app\admin\model\Announcement;
use think\Db;
use think\Exception;

/**
 * 案例接口
 */
class Coupon extends Api
{
    protected $noNeedLogin = [];  //存在则不验证token

	public function _initialize(){
		$action = request()->action();
		$ignore = array('host','accept','content-length','content-type');
		$headers = array();
		if($this->request->request('token')){
			$headers['token'] = $this->request->request('token');
		}else{
			foreach($_SERVER as $key=>$value){
				if(substr($key, 0, 5)==='HTTP_'){
					$key = substr($key, 5);
					$key = str_replace('_', ' ', $key);
					$key = str_replace(' ', '-', $key);
					$key = strtolower($key);
					if(!in_array($key, $ignore)){
						$headers[$key] = $value;
					}
				}
			}
		}
		if(empty($headers['token']) && in_array($action,$this->noNeedLogin)){
			//当未传token并且控制器可不不传token情况下不需要验证登录
		}else{
			$token=$headers['token'];
			$this->user=User::where(array('token'=>$token))->find();
			if(!$this->user){
				$this->error('请登录');
			}
            if($this->user['status'] != 1){
                $this->error('账号异常');
            }
			User::where(array('id'=>$this->user['id']))->update(array('active_time'=>time()));
		}
		
       
	}


    //获取优惠卷列表
    public function getCouponList(){
        $type = $this->request->request('type',1); // 状态  1为获取可使用优惠卷  0 获取所有优惠卷
        $price = $this->request->request('price',0);
        $where = ['status'=>1,'user_id'=>$this->user['id']];
        
        $list = UserCoupon::where($where)->select();
        if($list){
            foreach ($list as $k=>&$v) {
                $where = ['id'=>$v['coupon_id'],'status'=>1];
                $where1 = ['id'=>$v['coupon_id'],'status'=>1];
                if($price){
                    $where['quota'] = ['<=',$price];
                    $where1['quota'] = 0;
                }
                $info = CouponModer::where($where)->whereOr(function($query) use ($where1){
                    $query->where($where1);
                })->find();
                if(!$info){
                    unset($list[$k]);
                    continue;
                }
                if($type == 1){
                    //判断是否在使用时间范围内
                    if($info['start_time'] > 0  && $info['start_time'] > time()){
                        //该优惠卷未开启
                        unset($list[$k]);
                    }
                    if($info['end_time'] > 0  && $info['end_time'] > time()){
                        //该优惠卷已经结束 过期了
                        unset($list[$k]);
                    }
                }

                $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
                if($info['start_time']){
                    $info['start_time'] = date('Y-m-d H:i:s',$info['start_time']);
                }
                if($info['end_time']){
                    $info['end_time'] = date('Y-m-d H:i:s',$info['end_time']);
                }
                $v['name'] = $info['name'];
                $v['price'] = $info['price'];
                $v['start_time'] = $info['start_time'];
                $v['end_time'] = $info['end_time'];
                $v['quota'] = $info['quota'];
                $v['isCheck'] = false;
            }
            $list = array_values($list);

        }

        $this->success('操作成功',$list);
    }

    

}